// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.Moodles.MoodleType;
import zombie.characters.skills.PerkFactory;
import zombie.core.Core;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.debug.DebugOptions;
import zombie.iso.IsoDirections;
import zombie.iso.objects.IsoWindow;

public final class CloseWindowState extends State {
    private static final CloseWindowState _instance = new CloseWindowState();

    public static CloseWindowState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        owner.setIgnoreMovement(true);
        owner.setHideWeaponModel(true);
        IsoWindow isoWindow = (IsoWindow)hashMap.get(0);
        if (Core.bDebug && DebugOptions.instance.CheatWindowUnlock.getValue()) {
            isoWindow.Locked = false;
            isoWindow.PermaLocked = false;
        }

        if (isoWindow.north) {
            if ((float)isoWindow.getSquare().getY() < owner.getY()) {
                owner.setDir(IsoDirections.N);
            } else {
                owner.setDir(IsoDirections.S);
            }
        } else if ((float)isoWindow.getSquare().getX() < owner.getX()) {
            owner.setDir(IsoDirections.W);
        } else {
            owner.setDir(IsoDirections.E);
        }

        owner.setVariable("bCloseWindow", true);
        owner.clearVariable("BlockWindow");
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        if (owner.getVariableBoolean("bCloseWindow")) {
            IsoPlayer isoPlayer = (IsoPlayer)owner;
            if (isoPlayer.pressedMovement(false) || isoPlayer.pressedCancelAction()) {
                owner.setVariable("bCloseWindow", false);
            } else if (!(hashMap.get(0) instanceof IsoWindow)) {
                owner.setVariable("bCloseWindow", false);
            } else {
                IsoWindow isoWindow = (IsoWindow)hashMap.get(0);
                if (isoWindow != null && isoWindow.getObjectIndex() != -1) {
                    isoPlayer.setCollidable(true);
                    isoPlayer.updateLOS();
                    if (isoWindow.north) {
                        if ((float)isoWindow.getSquare().getY() < owner.getY()) {
                            owner.setDir(IsoDirections.N);
                        } else {
                            owner.setDir(IsoDirections.S);
                        }
                    } else if ((float)isoWindow.getSquare().getX() < owner.getX()) {
                        owner.setDir(IsoDirections.W);
                    } else {
                        owner.setDir(IsoDirections.E);
                    }
                } else {
                    owner.setVariable("bCloseWindow", false);
                }
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.clearVariable("BlockWindow");
        owner.clearVariable("bCloseWindow");
        owner.clearVariable("CloseWindowOutcome");
        owner.clearVariable("StopAfterAnimLooped");
        owner.setIgnoreMovement(false);
        owner.setHideWeaponModel(false);
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        HashMap hashMap = owner.getStateMachineParams(this);
        if (owner.getVariableBoolean("bCloseWindow")) {
            if (!(hashMap.get(0) instanceof IsoWindow)) {
                owner.setVariable("bCloseWindow", false);
            } else {
                IsoWindow isoWindow = (IsoWindow)hashMap.get(0);
                if (event.m_EventName.equalsIgnoreCase("WindowAnimLooped")) {
                    if ("start".equalsIgnoreCase(event.m_ParameterValue)) {
                        int _int = Math.max(5 - owner.getMoodles().getMoodleLevel(MoodleType.Panic), 1);
                        if (!isoWindow.isPermaLocked() && isoWindow.getFirstCharacterClimbingThrough() == null) {
                            owner.setVariable("CloseWindowOutcome", "success");
                        } else {
                            owner.setVariable("CloseWindowOutcome", "struggle");
                        }

                        return;
                    }

                    if (event.m_ParameterValue.equalsIgnoreCase(owner.getVariableString("StopAfterAnimLooped"))) {
                        owner.setVariable("bCloseWindow", false);
                    }
                }

                if (event.m_EventName.equalsIgnoreCase("WindowCloseAttempt")) {
                    this.onAttemptFinished(owner, isoWindow);
                } else if (event.m_EventName.equalsIgnoreCase("WindowCloseSuccess")) {
                    this.onSuccess(owner, isoWindow);
                }
            }
        }
    }

    @Override
    public boolean isDoingActionThatCanBeCancelled() {
        return true;
    }

    private void onAttemptFinished(IsoGameCharacter isoGameCharacter, IsoWindow isoWindow) {
        this.exert(isoGameCharacter);
        if (isoWindow.isPermaLocked()) {
            isoGameCharacter.getEmitter().playSound("WindowIsLocked", isoWindow);
            isoGameCharacter.setVariable("CloseWindowOutcome", "fail");
            isoGameCharacter.setVariable("StopAfterAnimLooped", "fail");
        } else {
            int _int = Math.max(5 - isoGameCharacter.getMoodles().getMoodleLevel(MoodleType.Panic), 3);
            if (!isoWindow.isPermaLocked() && isoWindow.getFirstCharacterClimbingThrough() == null) {
                isoGameCharacter.setVariable("CloseWindowOutcome", "success");
            } else {
                isoGameCharacter.setVariable("CloseWindowOutcome", "struggle");
            }
        }
    }

    private void onSuccess(IsoGameCharacter isoGameCharacter, IsoWindow isoWindow) {
        isoGameCharacter.setVariable("StopAfterAnimLooped", "success");
        IsoPlayer.getInstance().ContextPanic = 0.0F;
        if (isoWindow.getObjectIndex() != -1 && isoWindow.open) {
            isoWindow.ToggleWindow(isoGameCharacter);
        }
    }

    private void exert(IsoGameCharacter isoGameCharacter) {
        float _float = zombie.GameTime.getInstance().getMultiplier() / 1.6F;
        switch (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Fitness)) {
            case 1:
                isoGameCharacter.exert(0.01F * _float);
                break;
            case 2:
                isoGameCharacter.exert(0.009F * _float);
                break;
            case 3:
                isoGameCharacter.exert(0.008F * _float);
                break;
            case 4:
                isoGameCharacter.exert(0.007F * _float);
                break;
            case 5:
                isoGameCharacter.exert(0.006F * _float);
                break;
            case 6:
                isoGameCharacter.exert(0.005F * _float);
                break;
            case 7:
                isoGameCharacter.exert(0.004F * _float);
                break;
            case 8:
                isoGameCharacter.exert(0.003F * _float);
                break;
            case 9:
                isoGameCharacter.exert(0.0025F * _float);
                break;
            case 10:
                isoGameCharacter.exert(0.002F * _float);
        }
    }

    public IsoWindow getWindow(IsoGameCharacter owner) {
        if (!owner.isCurrentState(this)) {
            return null;
        } else {
            HashMap hashMap = owner.getStateMachineParams(this);
            return (IsoWindow)hashMap.get(0);
        }
    }
}
